// Justin Cowen
// CSC 332 - Data Structures
// March 12, 2010
// Program #3
// This program uses the MyArrayList class and the various methods associated with it
// to read in an infix expression and output the result in a table in line with the
// order of operations

import java.io.*;

class tableCreate{ //this class creates the output table with the results
	
    char charResult = (char)(int)'z'+1;  //place holder for z,y, etc. result 
    MyArrayList <Character>operatorStack = new MyArrayList<Character>(); 
    MyArrayList <Character>characterStack = new MyArrayList<Character>(); //operand stack
	
    public tableCreate(String userInput){  //creates the print out of the output table
        System.out.println("Operator\tOperand1\tOperand2\tResult"); //table title
        for(int i=0; i<userInput.length(); i++){
            readInChars(userInput.charAt(i));
        }
        while(operatorStack.isEmpty()==false){ //reached the end of input but operator stack 
            char temp = operatorStack.pop();   //still isn't empty
            printResult(temp);
        }
    }

    public static int precedenceTest(char operator) // test of precedence order
    {
	    if(operator=='/'||operator=='*')
            return 2;
	    else if(operator=='+'||operator=='-')
	        return 1;
	    else
	        return 0;
    }
    
    public void readInChars(char currentCharacter){ 
        if (currentCharacter==' '){
            return;
        }
        else if(currentCharacter=='*'||currentCharacter=='/'||currentCharacter=='+'||currentCharacter=='-'){
            if (operatorStack.isEmpty()){
                operatorStack.push(currentCharacter);
            }
            else{
                char optemp = operatorStack.get(0);
                if(precedenceTest(currentCharacter)>precedenceTest(optemp)){
                    operatorStack.push(currentCharacter);
                }
                else if(precedenceTest(currentCharacter)<=precedenceTest(optemp))
                {
                    optemp = operatorStack.pop();
                    printResult(optemp);
                    readInChars(currentCharacter); //recursive call in order to check currentCharacter again
                }
            } 	
        }
    	else if(currentCharacter>='a'&& currentCharacter<='z'){
    		characterStack.push(currentCharacter);
    	}
    	else{
    		System.out.println("Entry not a valid operator or Operand: Terminating Program");
    	}
    }
    
    public void printResult(char topOperatorStack){ //print output line
        char tempOperator = topOperatorStack;
        char tempCharacter = characterStack.pop();
        char tempCharacter2 = characterStack.pop();
        charResult = (char)((int)charResult -1);
        characterStack.push(charResult); //result character decremented
        System.out.println(tempOperator+"\t\t"+tempCharacter2+"\t\t"+tempCharacter+"\t\t"+charResult);
    }
}
public class Prog3 { //main method
    public static void main(String[] args){
        BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in));
        String line = null; 

        while(line==null){
            System.out.print("Enter an expression: ");
            try{
                line = keyboard.readLine();
            }
            catch(IOException e){}
            if (line.equals("end"))
                line=null;
            tableCreate test = new tableCreate(line); //instance of tableCreate
        }
    }
}

